package com.videomeetting.legend.videomeetting.activity.utils;

import android.annotation.TargetApi;
import android.net.ConnectivityManager;
import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by legend on 18-7-13.
 */

public class EthernetInfo {
    final String TAG = "EthernetInfo";
    public String ipAddress = null;
    public String maskAddress = null;
    public String gatewayAddress = null;
    public String dns1Address = null;
    public String dns2Address = null;

    public  void getLocalIpAddress() {
        Process cmdProcess = null;
        BufferedReader reader = null;
        try {
            cmdProcess = Runtime.getRuntime().exec("ifconfig");
            reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
            String line = null;
            line=reader.readLine();
            while ((line = reader.readLine())!=null) {
                    if(line.startsWith("eth0")){
                        String readLine = reader.readLine();
                        Log.i(TAG, "getLocalIpAddress: "+readLine);
                        String[] split = readLine.split(":");
                        ipAddress = split[1].split("  ")[0];
                        maskAddress = split[3];
                        System.out.println(split[1].split("  ")[0]);
                    }
                // 一次读入一行数据
            }

        } catch (Exception e) {
//            return null;
            e.printStackTrace();
        } finally{
            try {
                reader.close();
            } catch (IOException e) {
            }
            cmdProcess.destroy();
        }
    }
    public String getLocalDNS(){
        Process cmdProcess = null;
        BufferedReader reader = null;
        String dnsIP = "";
        try {
            cmdProcess = Runtime.getRuntime().exec("getprop net.dns1");
            reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
            dnsIP = reader.readLine();
            return dnsIP;
        } catch (IOException e) {
            return null;
        } finally{
            try {
                reader.close();
            } catch (IOException e) {
            }
            cmdProcess.destroy();
        }
    }

    private String calcMaskByPrefixLength(int length) {
        int mask = -1 << (32 - length);
        int partsNum = 4;
        int bitsOfPart = 8;
        int maskParts[] = new int[partsNum];
        int selector = 0x000000ff;


        for (int i = 0; i < maskParts.length; i++) {
            int pos = maskParts.length - 1 - i;
            maskParts[pos] = (mask >> (i * bitsOfPart)) & selector;
        }


        String result = "";
        result = result + maskParts[0];
        for (int i = 1; i < maskParts.length; i++) {
            result = result + "." + maskParts[i];
        }
        return result;
    }
}
